<template>
  <div class="search-bar">
    <div
      class="search-bar-float"
      :class="(suggestions.length > 0 && isFocus) || isHover ? '__smart' : ''"
    >
      <div class="input">
        <div
          v-if="searchButtonPosition === 'start'"
          class="button"
          :class="searchButtonHover ? 'use-hover' : null"
        >
          <Icon name="search" :size="25" fill-color="var(--text-color)"></Icon>
        </div>
        <TextInput
          class="text-input"
          :placeholder="placeholder"
          :show-clear="showClear"
          :confirm-before-clear="cofrimBeforeClear"
          v-model="value"
          @update="onUpdate"
          @focus="onFocus"
        >
        </TextInput>
        <div
          v-if="searchButtonPosition !== 'start'"
          class="button"
          :class="searchButtonHover ? 'use-hover' : null"
        >
          <Icon name="search" :size="25" fill-color="var(--text-color)"></Icon>
        </div>
      </div>
      <div
        v-if="!!lazyLoad"
        class="loading"
        v-show="loading && suggestions.length === 0 && value.length > 0"
      >
        <RotateLoading />
      </div>
      <div
        v-if="!!lazyLoad"
        @mousemove="isHover = true"
        @mouseleave="isHover = false"
        class="suggestion"
      >
        <div
          @click.stop.prevent="chose(item)"
          v-for="(item, index) in suggestions"
          :key="index"
          class="item use-hover"
        >
          <KeywordHighlight :keyword="value" :value="item.label" />
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import Icon from '@/components/a/AfastIcon.vue'
import TextInput from '@/components/a/TextInput.vue'
import { ref } from 'vue'
import RotateLoading from '@/components/a/RotateLoading.vue'
import useThrottle from '@/hooks/useDebounce'
import KeywordHighlight from '@/components/a/KeywordHighlight.vue'
const loading = ref(false)
const value = ref('')
const isFocus = ref(false)
const isHover = ref(false)
const throttle = useThrottle()
const chose = (item: { label: string }) => {
  value.value = item.label
  emits('chose', item)
}
const onFocus = (v: boolean) => {
  isFocus.value = v
}
const emits = defineEmits(['chose'])
const suggestions = ref<{ label: string }[]>([])
const props = defineProps({
  //显示清除
  showClear: {
    type: Boolean,
    default: false
  },
  //清除前询问
  cofrimBeforeClear: {
    type: Boolean,
    default: false
  },
  //提示
  placeholder: {
    type: String,
    default: '输入关键词'
  },
  //懒加载函数
  lazyLoad: {
    type: Function
  },
  //按钮位置 start end
  searchButtonPosition: {
    type: String,
    default: 'start'
  },
  //按钮鼠标悬停效果
  searchButtonHover: {
    type: Boolean,
    default: false
  }
})
//文本更新事件
const onUpdate = (v: string) => {
  if (v.length === 0) {
    suggestions.value = []
    return
  }

  loading.value = true
  //懒加载
  throttle(() => {
    if (props.lazyLoad) {
      props.lazyLoad(v, (list: any[]) => {
        if (value.value === v) {
          loading.value = false
          suggestions.value = list
        }
      })
    }
  })
}
</script>
<style lang="scss">
.search-bar {
  position: relative;
  width: 300px;
  height: 40px;

  .search-bar-float {
    z-index: 0;
    position: absolute;
    width: 300px;
    border-radius: 5px;
    background: var(--blank-color);
    border: 1px solid var(--border-color);

    .input {
      padding: 0 10px;
      display: flex;
      align-items: center;
      height: 40px;

      .text-input {
        width: 100%;
      }

      .button {
        flex-shrink: 0;
        width: 30px;
        height: 30px;
        background: var(--default-color);
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        border-radius: 5px;
      }

      .button:last-child {
        margin-left: 10px;
      }
    }

    .loading {
      z-index: 1000;
      width: 100%;
      height: 50px;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .suggestion {
      z-index: 1000;
      position: relative;
      width: 100%;
      overflow: hidden;
      max-height: 0;

      .item {
        width: 100%;
        cursor: pointer;
        padding: 0 15px;
        height: 35px;
        background: var(--default-color);
        display: flex;
        align-items: center;
        transition: 0.5s;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
  }

  .search-bar-float.__smart {
    box-shadow: 1px 1px 10px #eee;

    .suggestion {
      margin: 10px 0;
      max-height: 500px;
      transition: 0.5s;
    }
  }

  .search-bar.__slot {
    box-shadow: 1px 1px 10px #eee;

    .slot {
      transition: 0.5s;
      max-height: 200px;
    }
  }
}
</style>
